/**
 * Copyright (C) 2011 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2011 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.base.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;

import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import wzw.swing.StatusBar;
import wzw.util.ComponentUtil;
import wzw.util.I18nUtil;
import wzw.util.I18nUtils;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.calendar.ui.CalendarToolBar;
import com.kingcore.director.core.mail.MailSend;
import com.kingcore.director.core.util.DirectorComponentUtils;
import com.kingcore.director.main.Main;
import com.kingcore.director.main.MainFrame;
import com.kingcore.director.reminder.ui.AwokeEditDialog;
import com.kingcore.director.resourcemaster.ui.ResourceMasterMainPanel;
import com.kingcore.director.setting.encoding.StringEncoding;
import com.kingcore.director.setting.ui.AboutDialog;
import com.kingcore.director.setting.ui.ClassConfigDialog;
import com.kingcore.director.setting.ui.FileBackupDialog;
import com.kingcore.director.setting.ui.PreferenceDialog;
import com.kingcore.framework.context.ApplicationContext;

/**
 * <p> Basic class for all Director JPanel. </p>
 * @author Zeven/wzw on Mar 3, 2009
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public abstract class DirectorPanel extends JPanel 
		implements ActionListener, Observer  {

	//log object
	private final static Logger log = Logger.getLogger(DirectorPanel.class);

	protected MainFrame mainFrame = null;
	protected DirectorMenuBar menuBar = null; 
	protected DirectorToolBar toolBar = null; 
	protected DirectorFrame baseFrame = null;

	protected JMenuItem jpmi_MainTable_AddFileOpenTaskAwoke;
	
	public JMenuItem getAddFileOpenTaskAwokeMenuItem(){
		if (jpmi_MainTable_AddFileOpenTaskAwoke==null) {
			jpmi_MainTable_AddFileOpenTaskAwoke = new JMenuItem();
			jpmi_MainTable_AddFileOpenTaskAwoke.setText("Add File to Task");
			jpmi_MainTable_AddFileOpenTaskAwoke.addActionListener( this );
			//jpmi_MainTable_AddFileOpenTaskAwoke.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_D,Event.CTRL_MASK) );
		}
		return jpmi_MainTable_AddFileOpenTaskAwoke;
	}
	
	public DirectorPanel(DirectorFrame baseFrame) {
		super();
		log.debug("constructor of DirectorPanel.");
		this.mainFrame = (MainFrame) baseFrame;
		this.baseFrame = baseFrame;

		this.addComponentListener(new ComponentAdapter() {
			public void componentShown(ComponentEvent e) {
				log.debug("panel:shown");
				//refreshView();  // 不建议这调用，事件不可靠，在上层tabpage切换时候主动调用。
				//thisResized(e);
		    	statusBarResize();  //call StatusBar resize
			}
		});
	}

	/**
	 * 
	 * <p>子类可以override，实现构造之后的一些方法。</p>
	 * @author Zeven on Jun 3, 2012
	 */
	protected void constructAfter() {
    	statusBarResize();  //call StatusBar resize
    	this.repaint();
		this.validate();  //wzw:这句保证作为默认面板时候，statusBar显示
		this.revalidate();
	}
	
	
	protected void statusBarResize(){
		
		
		if (statusBar==null || this.isStatusBarShow==false ) {
			return;
		}
//		int width = this.getWidth();  
//		double[] rates = StatusBar_Item_Width_Rates;
//
//		StatusBar l_statusBar = getStatusBar();
//		int cellCount = l_statusBar.getCellCount();
//		JLabel[] label = new JLabel[cellCount];
//		String[] txts = new String[cellCount];
//		for (int i = 0; i < cellCount; i++) {
//			//System.out.println("width new="+width+"cellCount"+i);
//			txts[i] = l_statusBar.getStatusText(i);
////			label[i]  = l_statusBar.getLabel(i);
////			System.out.println( label[i].getPreferredSize().getWidth() );
////			int w = (int)(width*rates[i]);
////			label[i].setPreferredSize(new Dimension(w, getPreferredSize().height));
////			label.getPreferredSize().setSize(width*rates[i], getPreferredSize().height);
////			System.out.println( w+"  "+label[i].getPreferredSize().getWidth()
////					  + " "+label[i].getText() );
//		}
////		this.getStatusBar().revalidate();		
//		statusBar = new StatusBar();  // refer to new object.
		
		//if size changed,create a new status bar
		StatusBar oldStatusBar = this.getStatusBar();
		this.remove(this.getStatusBar());  //remove before add a new one.
		this.add( createStatusBar() , java.awt.BorderLayout.SOUTH);
		statusBar = this.getStatusBar();
		int cellCount = statusBar.getCellCount();
		for (int i = 0; i < 3; i++) {  //cellCount
			statusBar.setStatus(i, oldStatusBar.getStatusText(i) );
		}
		//mainContentPane.remove(2);
		
		//mainContentPane.repaint();
		
		this.revalidate();
		
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * <p>java方法的说明...</p>
	 * @author Zeven on Mar 4, 2011
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	//与Panel内容关联的外部基本对象
	public DirectorMenuBar getMenuBar(){
		return (DirectorMenuBar)this.mainFrame.getJMenuBar();
	}
	
	/**
	 * 默认都使用CalendarToolBar，可以根据需要定制。
	 * @return
	 */
	public DirectorToolBar getToolBar(){
		return getToolBar(false);
	}
	
	public DirectorToolBar getToolBar(Boolean createWhenNull){		
		if (this.toolBar==null && createWhenNull) {
			createToolBar( );
			//this.setPreferredSize(new Dimension(20,25));
		}
		return this.toolBar; 
	}

	protected DirectorToolBar createToolBar(){
		this.toolBar = new CalendarToolBar( this );
		return this.toolBar; 
	}
	
	private static double[] StatusBar_Item_Width_Rates={3d/10, 1d/5, 3d/20, 5d/20, 1d/10};
	private static String[] StatusBar_Item_Text={"就绪", "系统管家", "泽文堂出品", "", ""};

	protected StatusBar statusBar = null;
	/**
	 * return current status bar object.
	 * @return
	 */
	public StatusBar getStatusBar() {
		return getStatusBar(false);
	}
	
	public StatusBar getStatusBar(Boolean createWhenNull) {
		if (statusBar == null || createWhenNull) {
			statusBar = createStatusBar();
//			statusBar.revalidate();
//			statusBar.repaint();
//			statusBar.validate();
			//this.getLanguageManager().addObserver(statusBar);
		}
		return statusBar;
	}

	protected StatusBar createStatusBar() {
		statusBar = new StatusBar();
		//statusBar.setEditable(false);
		int width = Main.MainFrame_Size_Width; //wzw:big bug here //920;//this.getWidth();  //DirectorContext.getMainFrameWidth();
		double[] rates = StatusBar_Item_Width_Rates;
		String[] txts = StatusBar_Item_Text;
		
		for (int i = 0; i < StatusBar_Item_Width_Rates.length; i++) {
			if(i==3){
				statusBar.addStatusCell2((int)(width*rates[i]), this.mainFrame); 
				//statusBar.set
			}else{
				statusBar.addStatusCell((int)(width*rates[i]));
				statusBar.setStatus(i, txts[i]);
			}
		}
//			statusBar.addStatusCell(width*2/5); //200);
//			statusBar.addStatusCell(width*1/5); //150);
//			statusBar.addStatusCell(width*1/5); //150);
//			statusBar.addStatusCell(width*1/5); //180);
//			statusBar.setStatus(0,"就绪");
//			statusBar.setStatus(1,"系统管家");
//			statusBar.setStatus(2,"泽文堂出品");
//			statusBar.setStatus(3,"");
		//statusBarResize();
		statusBar.revalidate();  //刷新并显示
		statusBar.validate();
		
		//创建一个定时广告切换
//		JEditorPane jep = (JEditorPane) getComponent(3);
//		try {
//			jep.setPage( AdUtil.getAdUrlByType("awkmsg") );
//		} catch (IOException e) {
//			log.error(e);
//		}
		
		return statusBar;
		
	}
	
	public void actionPerformed(ActionEvent event) {
		Object obj=event.getSource();
		// toolBar and menuBar
		// 添加这一段，处理公共工具栏，避免this.menuBar==null导致的异常
		if( obj== this.toolBar.tb_fileExplorer) { 
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_FileExplorer_Index );
			return;
		} else if( obj== this.toolBar.tb_fileCopy){  // for menu and toolbar
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_FileCopy_Index );
			return;
		} else if( obj== this.toolBar.tb_favoriteFile){
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_FavoriteFiles_Index );
			return;
		} else if( obj== this.toolBar.tb_programLibrary){
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_ProgramLibrary_Index);
			return;
		} else if( obj== this.toolBar.tb_documentLibrary){
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_DocumentLibrary_Index );
			return;
		} 
		
		if (this.menuBar==null) {
			return; 
		}
		if( obj== this.toolBar.tb_exitSys 
				 || ( obj==this.menuBar.jmi_File_Exit) ){					//退出系统
			this.mainFrame.executeExitSystem();
			
		}else 
		if( this.menuBar!=null && obj == this.menuBar.jmi_File_Restart ){					//Restart
			Main.restart();
			 
		} else if( obj== this.toolBar.tb_fileExplorer 
				  ||obj==this.menuBar.jmi_Tool_FileExplorer){ 
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_FileExplorer_Index );

		} else if( obj== this.toolBar.tb_fileCopy
					|| obj==this.menuBar.jmi_Tool_FileCopy){  // for menu and toolbar
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_FileCopy_Index );
			
		} else if( obj== this.toolBar.tb_favoriteFile
						|| obj==this.menuBar.jmi_Tool_FavoriteFiles){
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_FavoriteFiles_Index );
			 
		} else if( obj== this.toolBar.tb_programLibrary
						|| obj==this.menuBar.jmi_Tool_ProgramLibrary){
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_ProgramLibrary_Index);
			 
		} else if( obj== this.toolBar.tb_documentLibrary
						|| obj==this.menuBar.jmi_Tool_DocumentLibrary){
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_DocumentLibrary_Index );
			 
		} else if( obj==this.menuBar.jmi_Help_About){		//关于系统
			executeShowAbout();
			
//		} else if( obj==this.menuBar.jmi_Tool_SendMail ){ 	//发送邮件
//			executeSendMail();
		
		} else if( obj==this.menuBar.jmi_Tool_RefreshLocalSearch ){ 	//Refresh local search database
			if(JOptionPane.showConfirmDialog(this,"确认检索本地磁盘，刷新本地搜索数据库吗？(可能要花十几分钟或更长的时间)","提示",JOptionPane.OK_CANCEL_OPTION)
	 				!=JOptionPane.OK_OPTION)
				return;
			
			this.mainFrame.getLocalSearchMainPanel().executeRefreshLocalSearch();
			///JOptionPane.showMessageDialog(this,"执行磁盘索引创建完成！"); //多线程，故不能立即提示
			
		} else if( obj==this.menuBar.jmi_Tool_MyReminder ){ 
			this.mainFrame.getMainTabbedPane().setSelectedIndex( MainFrame.MainTabPane_ReminderMainPanel_Index );	
			 
		} else if( obj==this.menuBar.jmi_Tool_MyCalendar ){ 
			this.mainFrame.getMainTabbedPane().setSelectedIndex( MainFrame.MainTabPane_CalendarMainPanel_Index );	
			 
		} else if( obj==this.menuBar.jmi_Tool_ResourceMaster ){ 
			DirectorComponentUtils.openTabpage(this.mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			
		} else if( obj==this.menuBar.jmi_Tool_LocalSearch ){
			//executeLocalSearch();
			this.mainFrame.getMainTabbedPane().setSelectedIndex( MainFrame.MainTabPane_LocalSearchMainPanel_Index );	
			 
//		} else if( obj==this.menuBar.jmi_Tool_MyNoteBook ){
//			//executeLocalSearch();
//			this.mainFrame.getMainTabbedPane().setSelectedIndex( MainFrame.MainTabPane_NoteBookMainPanel_Index );	
			 
		} else if( obj==this.menuBar.jmi_Tool_MyNotePaper ){
			//executeLocalSearch();
			this.mainFrame.getMainTabbedPane().setSelectedIndex( MainFrame.MainTabPane_NotePaperMainPanel_Index );	
			 
		} else if( obj==this.menuBar.jmi_Init_Flxx ){ 	//
			execInitFlxx();
			
		} else if( obj==this.menuBar.jmi_Init_Filebackup ){ 	//复制文件
			execInitFilebackup();
			
//		} else if( obj==this.menuBar.jmi_Init_Mail ){ 	//复制文件
//			execInitMail();
			
		} else if( obj==this.menuBar.jmi_View_ToolBar){		//show/hide ToolBar
			DirectorPanel t_currentPanel = (DirectorPanel) this.mainFrame.getMainTabbedPane().getSelectedComponent();
			t_currentPanel.executeShowToolBar(this.menuBar.jmi_View_ToolBar.isSelected());
			
		}  else if( obj==this.menuBar.jmi_Tool_Preference){		//show/hide ToolBar
			executeShowPreference();
			
		}  else if( obj==this.menuBar.jmi_Tool_OpenNoDisturb){ 
			mainFrame.setOpenNoDisturbState(Main.isNoDisturbOpen);
			//Main.isNoDisturbOpen = !Main.isNoDisturbOpen;
			
		} else if( obj==this.menuBar.jmi_View_StatusBar){		//show/hide StatusBar
			DirectorPanel t_currentPanel = (DirectorPanel) this.mainFrame.getMainTabbedPane().getSelectedComponent();
			t_currentPanel.excecuteShowStatusBar(this.menuBar.jmi_View_StatusBar.isSelected());
				
		} else if(obj==this.menuBar.jmi_View_LookAndFeel_Windows) {
			ComponentUtil.setLookAndFeel( DirectorConstants.LookAndFeel_Windows ,this.mainFrame);
			//System.out.println("UPDATE "+DirectorConstants.SYS_Setting+" SET VAL='"+ComponentUtil.LOOKANDFEEL_WINDOWS+"' WHERE KEY='"+DirectorConstants.System_LookAndFeel+"'");
			doUpdate("UPDATE "+DirectorConstants.SYS_Setting+" t SET t.VAL='"+DirectorConstants.LookAndFeel_Windows
							+"' WHERE KEY='"+DirectorConstants.System_LookAndFeel_Key+"'");
			
		} else if(obj==this.menuBar.jmi_View_LookAndFeel_Metal) {
			ComponentUtil.setLookAndFeel( DirectorConstants.LookAndFeel_Metal, this.mainFrame);
			doUpdate("UPDATE "+DirectorConstants.SYS_Setting+" t SET t.VAL='"+DirectorConstants.LookAndFeel_Metal
							+"' WHERE KEY='"+DirectorConstants.System_LookAndFeel_Key+"'");
			System.out.println("done");
			
		} else if(obj==this.menuBar.jmi_View_LookAndFeel_Motif) {
			ComponentUtil.setLookAndFeel( DirectorConstants.LookAndFeel_Motif, this.mainFrame);
			doUpdate("UPDATE "+DirectorConstants.SYS_Setting+" t SET t.VAL='"+DirectorConstants.LookAndFeel_Motif
							+"' WHERE KEY='"+DirectorConstants.System_LookAndFeel_Key+"'");
			
		} else if(obj==this.menuBar.jmi_View_Language_ChineseCN) {
			this.mainFrame.executeChangeLanguage( Locale.SIMPLIFIED_CHINESE );
			doUpdate("UPDATE "+DirectorConstants.SYS_Setting+" t SET t.VAL='"+DirectorConstants.System_Language_CN
							+"' WHERE KEY='"+DirectorConstants.System_Language_Key+"'");
			
		} else if(obj==this.menuBar.jmi_View_Language_ChineseTW) {
			this.mainFrame.executeChangeLanguage( Locale.TAIWAN );
			doUpdate("UPDATE "+DirectorConstants.SYS_Setting+" t SET t.VAL='"+DirectorConstants.System_Language_TW
							+"' WHERE KEY='"+DirectorConstants.System_Language_Key+"'");
			
			
		} else if(obj==this.menuBar.jmi_View_Language_English) {
			this.mainFrame.executeChangeLanguage( Locale.ENGLISH );
			//System.out.println("UPDATE "+DirectorConstants.SYS_Setting+" t SET t.VAL='English' WHERE KEY='"+DirectorConstants.System_Language+"'");
			doUpdate("UPDATE "+DirectorConstants.SYS_Setting+" t SET t.VAL='"+DirectorConstants.System_Language_EN
							+"' WHERE KEY='"+DirectorConstants.System_Language_Key+"'");
						
		} else if(obj==this.menuBar.jmi_View_Language_Japanese) {
			this.mainFrame.executeChangeLanguage( Locale.JAPANESE );
			//System.out.println("UPDATE "+DirectorConstants.SYS_Setting+" t SET t.VAL='English' WHERE KEY='"+DirectorConstants.System_Language+"'");
			doUpdate("UPDATE "+DirectorConstants.SYS_Setting+" t SET t.VAL='"+DirectorConstants.System_Language_JP
							+"' WHERE KEY='"+DirectorConstants.System_Language_Key+"'");
						
		} else if(obj==this.menuBar.jmi_Tool_Unicode) {
			executeUnicodeTranslate( );
			
		}
		
	}



	/**
	 * 添加文件打开提醒。默认2小时候，并且以2天位单位循环。
	 * @param fileUri
	 * @param fileName
	 */
	 public void executeAddNewFileOpenAwoke(String fileUri, String fileName) {
			Date t_date = Calendar.getInstance().getTime();
			t_date.setTime( t_date.getTime() + 1000*60*60*2); //2小时之后
			DateFormat df =  new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm"); //yyyy-MM-dd HH:mm:ss
			String timeStr = df.format(t_date) + ":00";
			
			StringBuffer content = new StringBuffer();
			content.append("<html>请点击打开：<br>\n<font size=\"3\">")
					.append("<a href=\"file://")
					.append(fileUri)
					.append("\">")
					.append(fileName)
					.append("</a></font><br>\n")
					.append("</html>");
			executeAddNewAwoke( "[文件]"+fileName, content.toString(), timeStr, "day", "2");
	 }
	 

	 public void executeAddNewContentAwoke(String content) {
			Date t_date = Calendar.getInstance().getTime();
			t_date.setTime( t_date.getTime() + 1000*60*60*2); //2小时之后
			DateFormat df =  new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm"); //yyyy-MM-dd :ss
			String timeStr = df.format(t_date) + ":00";
			
			String title = null;
			if (content!=null && content.length()>16) {
				title = content.substring(0,16);
			}else{
				title = content;
			}
			executeAddNewAwoke( title, content, timeStr, "day", "2");
	 }
	 
	/**
	 *  String title,
		String content,
		String beginTime,
		String interval,
		long intervalNum,
	 * <p>java方法的说明...</p>
	 * @author Zeven on Apr 17, 2011
	 * @param args
	 */
	 public void executeAddNewAwoke(String ...args) {
		//set as current panel.
		this.mainFrame.getMainTabbedPane().setSelectedIndex(MainFrame.MainTabPane_ReminderMainPanel_Index);
		AwokeEditDialog ap = new AwokeEditDialog(this.mainFrame );

		int i = 0;
		for(String arg : args) {
			i++;
			if (arg==null) {
				continue;
			}
			switch (i) {
			case 1:
				ap.getAwokeTitle().setText(arg);
				break;

			case 2:		
				ap.getContent().setText(arg);		
				break;

			case 3:
				ap.initBeginTime(arg);
				break;

			case 4:
				ap.getIsLoop().setSelected(true);
				if ("minute".equals(arg)) {
					ap.getMinuteRadio().setSelected(true);
				} else if ("hour".equals(arg)) {
					ap.getHourRadio().setSelected(true);
				} else if ("day".equals(arg)) {
					ap.getDayRadio().setSelected(true);
				} else if ("week".equals(arg)) {
					ap.getWeekRadio().setSelected(true);
				} else if ("month".equals(arg)) {
					ap.getMonthRadio().setSelected(true);
				} else if ("year".equals(arg)) {
					ap.getYearRadio().setSelected(true);
				}
				break;
				
			case 5:
				ap.getIntervalNum().setText(arg);
				break;
				
			default:
				break;
			}
		}
		
		ComponentUtil.setLocationToCenter(this.mainFrame, ap); //fix exception: this->this.mainFrame
		ap.setVisible(true);
		ap.getAwokeTitle().requestFocus();
		ap.getAwokeTitle().requestFocusInWindow();
	 }

	private StringEncoding encoding = null;
	
	/**
	 * 打开字符串编码转换JFrame窗口
	 *
	 */
	private void executeUnicodeTranslate() {
		if(encoding==null){
			encoding = new StringEncoding();
		}
		ComponentUtil.setLocationToCenter(encoding);
		encoding.setVisible( true );
		
	}
	
	/**
	 * 本类中的数据库更新操作方法
	 * @param sql
	 */
	private void doUpdate(String sql) {
		
		Connection conn= null;
		Statement stmt =null;
		ResultSet rs=null;
		try {
			conn= ApplicationContext.getInstance().getDataSourceManager().getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			conn.commit();
		} catch (Exception e){
			wzw.util.DbUtils.rollbackQuietly(conn);
			JOptionPane.showMessageDialog(this,e.getMessage(),"数据库操作失败",JOptionPane.ERROR_MESSAGE);
		} finally {
			wzw.util.DbUtils.closeQuietly(conn, stmt, rs);
		}
		
	}
	
	public boolean isToolBarShow=true;
	public AbstractAction showStatusBarAction;
	public boolean isStatusBarShow=true;
	
	 private void executeShowToolBar(Boolean isSelect) {
		 
		 //isToolBarShow = !isToolBarShow;
		 isToolBarShow = isSelect;
//		 this.menuBar.jmi_View_ToolBar.setSelected(isToolBarShow);
//		 this.toolBar.j20.setSelected( isToolBarShow );
		 
//		if(isToolBarShow && this.mainFrame.getToolBar()!=null){	//jmi_View_ToolBar.isSelected()
//			//after selected ,then ...
//			this.mainFrame.setToolBar(this.mainFrame.getToolBar());
////			this.mainFrame.getMainContentPane().add( this.mainFrame.getToolBar(), BorderLayout.NORTH);
//		}else if(this.mainFrame.getToolBar()!=null){
//			this.mainFrame.getMainTabbedPane().getMainContentPane().remove( this.mainFrame.getToolBar() );
//			DirectorPanel t_currentPanel = (DirectorPanel) this.mainFrame.getMainTabbedPane().getSelectedComponent();
//		}
		
		//DirectorPanel t_currentPanel = (DirectorPanel) this.mainFrame.getMainTabbedPane().getSelectedComponent();
		this.setToolBarVisible(isToolBarShow);
		
		this.mainFrame.getMainContentPane().doLayout();
		this.mainFrame.getReminderMainPanel().doLayout();
		
		//getMainContentPane().repaint();
		//getMainPanel().repaint();
		//getMainPanel().repaint();
		//getLeftScrollPane().doLayout();			
		//getTree().doLayout();
		//getMainPanel().repaint();
		
		this.mainFrame.getReminderMainPanel().doRefresh();	 
	 } 

	 /**
	  * do nothing here, implement in child class.
	  * @param isToolBarShow
	  */
	 public void setToolBarVisible(boolean isToolBarShow) {
		 this.isToolBarShow = isToolBarShow;
		 if (getToolBar()==null) {
				return;
		}
		if (isToolBarShow) {
			this.add( getToolBar(), BorderLayout.NORTH);
		}else{
			this.remove( getToolBar() );
		}
	}

	/**
	  * show Preference
	  *
	  */
	 private void executeShowPreference() {
		 PreferenceDialog prf = new PreferenceDialog( this.mainFrame );
		 ComponentUtil.setLocationToCenter( this.mainFrame, prf );
		 prf.setVisible(true);
	 } 
	 
	 
	 /**
	  * show/hide statusBar
	  *
	  */
	 private void excecuteShowStatusBar(Boolean isSelect){

		 //System.out.println("status bar");
		 
		 //isStatusBarShow = !isStatusBarShow;
		 isStatusBarShow = isSelect;
//		 this.menuBar.jmi_View_StatusBar.setSelected(isStatusBarShow);
//		 this.toolBar.j21.setSelected( isStatusBarShow );
		 
//		if( isStatusBarShow ){
//			this.mainFrame.getMainContentPane().add( this.mainFrame.getStatusBar(), BorderLayout.SOUTH);
//		}else{
//			this.mainFrame.getMainContentPane().remove( this.mainFrame.getStatusBar() );
//		}
		 //DirectorPanel t_currentPanel = (DirectorPanel) this.mainFrame.getMainTabbedPane().getSelectedComponent();
		 this.setStatusBarVisible(isStatusBarShow);
		 
		//this.mainFrame.getMainContentPane().doLayout();
		//this.mainFrame.getReminderMainPanel().doLayout();			
		
		//this.mainFrame.getReminderMainPanel().doRefresh();
//		leftScrollPanel.setViewportView(getTree());
//		rightScrollPanel.setViewportView(getTable());			
	}

	/**
	 * 
	 * <p>状态栏默认是有的，工具栏默认没有。</p>
	 * @author Zeven on Jun 2, 2012
	 * @param isStatusBarShow
	 */
	 public void setStatusBarVisible(boolean isStatusBarShow) {
		 this.isStatusBarShow = isStatusBarShow;
		 if (getStatusBar()==null) {
			return;
		}
		if (isStatusBarShow) {
			this.add( getStatusBar(), BorderLayout.SOUTH);
		}else{
			this.remove( getStatusBar() );
		}
	}

//		private AbstractAction getShowStatusBarAction(){
//			if(showStatusBarAction==null){
//				showStatusBarAction = new AbstractAction(){
////					public AbstractAction(){
////						super(null,new ImageIcon(str));
////					}
//					 
//					public void actionPerformed(ActionEvent e) {
//						//System.out.println("action 2");
//						excecuteShowStatusBar();
//					}
//					
//				};
//				//showToolBarAction.actionPerformed(e);
//			}
//			return showStatusBarAction;
//		}
	 /**
	  * 打开基本分类信息初始窗口。
	  *
	  */
	 private void execInitFlxx() {
		 ClassConfigDialog igx = new ClassConfigDialog(this.mainFrame);
		 ComponentUtil.setLocationToCenter( this.mainFrame, igx );
		 igx.setVisible( true );
	}

	 /**
	  * 打开文件备份信息初始窗口。
	  *
	  */
	 private void execInitFilebackup() {
		 FileBackupDialog ifb = new FileBackupDialog(this.mainFrame);
		 ComponentUtil.setLocationToCenter( this.mainFrame, ifb );
		 ifb.setVisible( true );
	}

	 /**
	  * 打开邮箱帐号信息初始窗口。
	  *
	  */
	 private void execInitMail() {
		 FileBackupDialog ifb = new FileBackupDialog(this.mainFrame);
		 ComponentUtil.setLocationToCenter( this.mainFrame, ifb );
		 ifb.setVisible( true );
	}
	
	 /**
	  * 发送邮件
	  *
	  */
	 private void executeSendMail() {
		 if(JOptionPane.showConfirmDialog(this,"确认发送邮件，而且配置好了属性文件了吗？","提示",JOptionPane.OK_CANCEL_OPTION)
		 				!=JOptionPane.OK_OPTION)
		 			return;
		// 复制文件
		 MailSend.setFilePath( System.getProperty(MainFrame.User_Data_Path));
		 try {
			MailSend.main(null);
		 } catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage() );
			return;
		 }
	}

	// 关于系统显示
	private void executeShowAbout() {
		AboutDialog da = new AboutDialog(this.mainFrame); /// this
		ComponentUtil.setLocationToCenter(this.mainFrame, da);

		/*da.setLocation(this.getLocationOnScreen().x
				+ (this.getWidth() - da.getWidth()) / 2, this
				.getLocationOnScreen().y
				+ (this.getHeight() - da.getHeight()) / 2);*/
		
		da.show();
	}

	protected final static int StatusBar_Index_One = DirectorFrame.StatusBar_Index_One;
	protected final static int StatusBar_Index_Two = DirectorFrame.StatusBar_Index_Two;
	protected final static int StatusBar_Index_Three = DirectorFrame.StatusBar_Index_Three;
	protected final static int StatusBar_Index_Four = DirectorFrame.StatusBar_Index_Four;
	protected final static int StatusBar_Index_Five = DirectorFrame.StatusBar_Index_Five;
	protected final static int StatusBar_Index_Six = DirectorFrame.StatusBar_Index_Six;
	
	
	protected String currentStatusInfor = "Ready"; // non static
	public void setStatus(int statusItemIndex, String content){
		currentStatusInfor = content;  //set as current info
//		baseFrame.getStatusBar().setStatus( statusItemIndex, currentStatusInfor); //change the display
		if(this.statusBar!=null){
			getStatusBar().setStatus( statusItemIndex, currentStatusInfor); //change the display
		}
	}

	/**
	 * 
	 * <p>refreshView for current panel,such as status bar,menu bar,tool bar ...</p>
	 * @author Zeven on 2011-4-6
	 */
	public void refreshView(){
		// please do something in subclass.
		if (getMenuBar()!=null) {
			getMenuBar().jmi_View_ToolBar.setSelected(this.isToolBarShow);
			getMenuBar().jmi_View_StatusBar.setSelected(this.isStatusBarShow);
			getMenuBar().revalidate();
			getMenuBar().repaint();
//			getMenuBar().validate();
		}
		//setStatus(StatusBar_Index_One, currentStatusInfor);
	}
	
	public abstract boolean fetchData() throws SQLException ;
	public abstract boolean saveData() throws SQLException ;
	
	// 定义变量便于国际化
	protected String msgDialog_plsSelectRow = null;        
	protected String msgDialog_info=null;              
	protected String msgDialog_infoExecuteSuccess = null;
	protected String msgDialog_infoExecuteFail = null;                    
	protected String msgDialog_infoDelete=null;
	protected String msgDialog_infoDeleteSuccess=null;             
	protected String msgDialog_error=null;                                 
	protected String msgDialog_errorDbQuery=null;                      
	protected String msgDialog_errorDbUpdate=null;   
	protected String msgDialog_infoChoosePath=null; 
	protected String msgDialog_infoSaveSuccess=null;
	protected String msgDialog_infoAdd2List=null;
	      
	protected String statusBar_creatingIndexFiles=null;
	protected String statusBar_ready=null;
	protected String statusBar_renameSuccess=null;
	protected String statusBar_saveSuccess=null;
	protected String statusBar_mainTableListInfo=null;
	protected String statusBar_createdIndexFilesSuccess=null;
	
	 /**
	  * 监听语言改变  所有子类采用 super.update 调用本方法。
	  */
	public void update(Observable o, Object arg) {
		
		//System.out.println(Locale.getDefault());
		String className = DirectorPanel.class.getName();
		ResourceBundle bundle = I18nUtils.getBundle(DirectorConstants.I18n_Language_Name);

		msgDialog_plsSelectRow = I18nUtil.getBundleString(bundle, className, "msgDialog_plsSelectRow.Message");    
		msgDialog_info = I18nUtil.getBundleString(bundle, className, "msgDialog_info.Title");    
		msgDialog_infoExecuteSuccess = I18nUtil.getBundleString(bundle, className, "msgDialog_infoExecuteSuccess.Message");
		msgDialog_infoExecuteFail = I18nUtil.getBundleString(bundle, className, "msgDialog_infoExecuteFail.Message");
		msgDialog_infoDelete = I18nUtil.getBundleString(bundle, className, "msgDialog_infoDelete.Message");    
		msgDialog_infoDeleteSuccess = I18nUtil.getBundleString(bundle, className, "msgDialog_infoDeleteSuccess.Message");    
		msgDialog_error = I18nUtil.getBundleString(bundle, className, "msgDialog_error.Title");    
		msgDialog_errorDbQuery = I18nUtil.getBundleString(bundle, className, "msgDialog_errorDbQuery.Title");    
		msgDialog_errorDbUpdate = I18nUtil.getBundleString(bundle, className, "msgDialog_errorDbUpdate.Title");    
		msgDialog_infoChoosePath = I18nUtil.getBundleString(bundle, className, "msgDialog_infoChoosePath.Message");    
		msgDialog_infoSaveSuccess = I18nUtil.getBundleString(bundle, className, "msgDialog_infoSaveSuccess.Message"); 
		msgDialog_infoAdd2List = I18nUtil.getBundleString(bundle, className, "msgDialog_infoAdd2List.Message");
		
		statusBar_creatingIndexFiles = I18nUtil.getBundleString(bundle, className, "statusBar_creatingIndexFiles.Text");     
		statusBar_createdIndexFilesSuccess = I18nUtil.getBundleString(bundle, className, "statusBar_createdIndexFilesSuccess.Text");     
		statusBar_ready = I18nUtil.getBundleString(bundle, className, "statusBar_ready.Text");     
		statusBar_renameSuccess = I18nUtil.getBundleString(bundle, className, "statusBar_renameSuccess.Text");  
		statusBar_saveSuccess = I18nUtil.getBundleString(bundle, className, "statusBar_saveSuccess.Text");  
		statusBar_mainTableListInfo = I18nUtil.getBundleString(bundle, className, "statusBar_mainTableListInfo.Text");
//		if ("Ready".equals(currentStatusInfor)) {
//			currentStatusInfor = statusBar_ready;
//		}
		getAddFileOpenTaskAwokeMenuItem().setText(I18nUtil.getBundleString(bundle, className, "jpmi_MainTable_AddFileOpenTaskAwoke.Text"));
		
		this.currentStatusInfor = statusBar_ready;
		//System.out.println( ConnectionPool.showTrace(8) );
		///setStatus(StatusBar_Index_One, currentStatusInfor); //子类可以再修改
		
		if(this.statusBar!=null){
			this.statusBar.setStatus(0, I18nUtil.getBundleString(bundle, MainFrame.class, "statusBar.one.Text"));
			this.statusBar.setStatus(1, I18nUtil.getBundleString(bundle, MainFrame.class, "statusBar.two.Text"));
			this.statusBar.setStatus(2, I18nUtil.getBundleString(bundle, MainFrame.class, "statusBar.three.Text"));
			//statusBarResize();
		}
	}
}
